package com.example.lwx;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class ShareActivity extends Activity {
    EditText et_name,et_pwd;
    Button btn_save,btn_read;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shared);
        btn_read=findViewById(R.id.readinfo);
        btn_save=findViewById(R.id.regist);
        et_name=findViewById(R.id.edname);
        et_pwd=findViewById(R.id.edpassword);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = et_name.getText().toString().trim();
                String pwd = et_pwd.getText().toString().trim();
                SharedPreferences sp = getSharedPreferences("data", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                editor.putString("user",name);
                editor.putString("pass",pwd);
                editor.commit();
                Toast.makeText(ShareActivity.this,"账号注册成功",Toast.LENGTH_LONG).show();
            }
        });
        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sp=getSharedPreferences("data",Context.MODE_PRIVATE);
                String name=sp.getString("user","admin");
                String pwd=sp.getString("pass","");
                Toast.makeText(ShareActivity.this,"读取账号信息\n"+name+" "+pwd,Toast.LENGTH_LONG).show();
            }
        });
    }
}





























